/**
 * 2013-6-23 上午1:10:52 Created By niexiaoqiang
 */

package com.xiaoqiang.util.mtwmail;

import java.security.AccessController;
import java.security.Provider;
import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @该类不推荐使用
 * @author NextSmile
 * 
 */
public class GMail extends Authenticator {
	private String mailhost = "smtp.gmail.com";
	private String user;
	private String password;
	private Session session;

	static {
		Security.addProvider(new JSSEProvider());
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(user, password);
	}

	public GMail() {
		this.user = "reportmtw";
		this.password = "nxq@198943";
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", mailhost);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");
		session = Session.getDefaultInstance(props, this);
	}

	public GMail(String user, String password) {
		this.user = user;
		this.password = password;
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", mailhost);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");
		session = Session.getDefaultInstance(props, this);
	}

	/**
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @param to
	 *            接收者
	 * @param filename
	 *            文件名
	 * @throws Exception
	 */
	public synchronized void sendMail(String subject, String content, String to, ArrayList<String> filenames) throws Exception {
		MimeMessage message = new MimeMessage(session);
		message.setSender(new InternetAddress(user + "@gmail.com"));
		// 添加主题
		message.setSubject(subject);
		/******** 内容和附件 ********/
		Multipart mp = new MimeMultipart();
		// 添加正文
		MimeBodyPart mbpContent = new MimeBodyPart();
		mbpContent.setContent(content, "text/html;charset=gb2312");
		mp.addBodyPart(mbpContent);
		// 添加附件
		for (String filename : filenames) {
			MimeBodyPart mbpFile = new MimeBodyPart();
			FileDataSource fds = new FileDataSource(filename);
			mbpFile.setDataHandler(new DataHandler(fds));
			mbpFile.setFileName(fds.getName());
			mp.addBodyPart(mbpFile);
		}
		message.setContent(mp);
		/******** 内容和附件 ********/
		// 设置日期
		message.setSentDate(new Date());
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		Transport.send(message);
	}

	/**
	 * @param subject
	 *            主题
	 * @param content
	 *            内容
	 * @param to
	 *            接收者
	 * @throws Exception
	 */
	public synchronized void sendMail(String subject, String content, String to) throws Exception {
		MimeMessage message = new MimeMessage(session);
		message.setSender(new InternetAddress(user + "@gmail.com"));
		// 添加主题
		message.setSubject(subject);
		/******** 内容和附件 ********/
		Multipart mp = new MimeMultipart();
		// 添加正文
		MimeBodyPart mbpContent = new MimeBodyPart();
		mbpContent.setContent(content, "text/html;charset=gb2312");
		mp.addBodyPart(mbpContent);
		/******** 内容和附件 ********/
		// 添加内容和附件
		message.setContent(mp);
		// 设置日期
		message.setSentDate(new Date());
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		Transport.send(message);
	}
}

final class JSSEProvider extends Provider {
	private static final long serialVersionUID = 1L;

	public JSSEProvider() {
		super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
		AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
			public Void run() {
				put("SSLContext.TLS", "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
				put("Alg.Alias.SSLContext.TLSv1", "TLS");
				put("KeyManagerFactory.X509", "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
				put("TrustManagerFactory.X509", "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
				return null;
			}
		});
	}
}